package com.tool.btn;

import cn.hutool.json.JSONUtil;
import com.tool.image.ImageMixDeal;
import com.tool.time.TimeLimit;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.AutoTaskJpanel;
import org.come.Jpanel.AutoTaskModelJpanel;
import org.come.bean.*;
import org.come.model.AutoTaskBase;
import org.come.mouslisten.HotKeyMouseListen;
import org.come.thread.AutoTaskTimeControlRunnable;
import org.come.thread.TimeControlRunnable;
import org.come.until.FormsManagement;
import org.come.until.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AutoTaskBtn extends MoBanBtn {
    private int caozuo;
    private AutoTaskJpanel autoTaskJpanel;
    private AutoTaskModelJpanel autoTaskModelJpanel;
    private int subscript;

    public AutoTaskBtn(String iconpath, int type, int caozuo, int subscript, AutoTaskJpanel autoTaskJpanel) {
        super(iconpath, type);
        this.caozuo = caozuo;
        this.subscript = subscript;
        this.autoTaskJpanel = autoTaskJpanel;
    }

    public AutoTaskBtn(String iconpath, int type, Color[] colors, String text, Font font, int caozuo,
                       AutoTaskModelJpanel autoTaskModelJpanel) {
        super(iconpath, type, colors);
        setText(text);
        setVerticalTextPosition(SwingConstants.CENTER);
        setHorizontalTextPosition(SwingConstants.CENTER);
        setFont(font);
        this.caozuo = caozuo;
        this.autoTaskModelJpanel = autoTaskModelJpanel;
    }

    @Override
    public void chooseyes() {
        // TODO Auto-generated method stub

    }

    @Override
    public void chooseno() {
        // TODO Auto-generated method stub

    }

    @Override
    public void nochoose(MouseEvent e) {
        if (caozuo == 1) {
            if(AutoTaskTimeControlRunnable.isIsAutoTask()){
                AutoTaskTimeControlRunnable.stopScript();
            }else {
                TimeLimit limit = TimeLimit.getLimits();
                TimeLimit.getLimits().getLimit("VIP");
                List<AutoTaskBase> stblist = new ArrayList();
                Map<Integer, AutoTaskModelJpanel> pmap = autoTaskJpanel.getMapAutoTaskModelPanel();
                RoleShow roleShow = ImageMixDeal.userimg.getRoleShow();
                if (TimeLimit.getLimits().getLimit("VIP")!=null) {
                    if (pmap != null && pmap.size() > 0) {
                        for (Map.Entry<Integer, AutoTaskModelJpanel> entry : pmap.entrySet()) {
                            AutoTaskModelJpanel autoTaskModelJpanel = entry.getValue();
                            if (autoTaskModelJpanel.getJCheckBox().isSelected()) {
                                AutoActiveBase activeBase = autoTaskModelJpanel.getActiveBase();
                                int autoNum = Integer.valueOf(autoTaskModelJpanel.getNumField().getText());
                                if (autoNum <= 0) {
                                    ZhuFrame.getZhuJpanel().addPrompt2("请输入自动任务执行次数");
                                    return;
                                }
                                if (roleShow.getTroop_id()!=null&&roleShow.getCaptian()!=1){
                                    ZhuFrame.getZhuJpanel().addPrompt2("不是队长不能启动");
                                    return;
                                }
                                AutoTaskBase autoTaskBase = new AutoTaskBase(activeBase, autoNum);
                                stblist.add(0, autoTaskBase);
                                //stblist.add(autoTaskBase);
                            }
                        }
                        if (stblist != null && stblist.size() > 0) {
                            AutoTaskTimeControlRunnable.addAutoTaskBaseList(stblist);
                            ImageIcon icon = new ImageIcon("inkImg/button/B459.png");
                            this.setIcon(icon);

                        AutoActiveBaseBean aar = new AutoActiveBaseBean();
                        aar.setAtlist(stblist);
                        String roleId = ImageMixDeal.userimg.getRoleShow().getRole_id().toString();
                        Util.writeAutoNum(roleId,JSONUtil.toJsonStr(aar));
                        } else {
                            ZhuFrame.getZhuJpanel().addPrompt2("请选择要自动执行的任务");
                            return;
                        }
                    } else {
                        ZhuFrame.getZhuJpanel().addPrompt2("请选择要自动执行的任务");
                        return;
                    }
                }else{
                    ZhuFrame.getZhuJpanel().addPrompt2("请开通月卡！");
                    return;
                }
            }
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        if (caozuo == 1) {

        } else if (caozuo == 2) {

        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        if (caozuo == 1) {
            FormsManagement.HideForm(46);//
        }
    }

}
